//
//  ZCRegisterTextField.m
//  ZCBaiSi
//
//  Created by zc on 16/7/11.
//  Copyright © 2016年 zc. All rights reserved.
//

#import "ZCRegisterTextField.h"

static NSString *const ZCPlaceholderColorKey = @"placeholderLabel.textColor";

@interface ZCRegisterTextField()<UITextFieldDelegate>

@end
@implementation ZCRegisterTextField

- (void)awakeFromNib{
    [super awakeFromNib];
    //设置光标颜色
    self.tintColor = [UIColor whiteColor];
    //设置默认的占位文字的颜色
    [self setValue:[UIColor grayColor] forKeyPath:ZCPlaceholderColorKey];
    //设置带有属性的placeHolder 富文本
//    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
//    attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
//    self.attributedPlaceholder = [[NSMutableAttributedString alloc]initWithString:self.placeholder attributes:attributes];
    //1.监听文字改变
//    [self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
//    [self addTarget:self action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
    //2.监听文字改变 代理监听 有一定的风险
  //  self.delegate = self;
    //3.通知监听文字改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:self];
    //4. 利用文本框重写响应者事件
    //成为第一响应者
//    - (BOOL)becomeFirstResponder{
//     [self setValue:[UIColor whiteColor] forKeyPath:ZCPlaceholderColorKey];
//        return [super becomeFirstResponder];
//    }
//    
//    //不做第一响应者
//    - (BOOL)resignFirstResponder{
//    [self setValue:[UIColor grayColor] forKeyPath:ZCPlaceholderColorKey];
//        return [super resignFirstResponder];
//    }

    
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)beginEditing{
    self.placeholderColor = [UIColor whiteColor];
 //   [self setValue:[UIColor whiteColor] forKeyPath:ZCPlaceholderColorKey];
}

- (void)endEditing{
    self.placeholderColor = [UIColor grayColor];
  //  [self setValue:[UIColor grayColor] forKeyPath:ZCPlaceholderColorKey];
}

- (void)editingDidBegin{
    [self setValue:[UIColor whiteColor] forKeyPath:ZCPlaceholderColorKey];
}

- (void)editingDidEnd{
    [self setValue:[UIColor grayColor] forKeyPath:ZCPlaceholderColorKey];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self setValue:[UIColor whiteColor] forKeyPath:ZCPlaceholderColorKey];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self setValue:[UIColor grayColor] forKeyPath:ZCPlaceholderColorKey];
}

//成为第一响应者
- (BOOL)becomeFirstResponder{
     [self setValue:[UIColor whiteColor] forKeyPath:ZCPlaceholderColorKey];
    return [super becomeFirstResponder];
}

    //不做第一响应者
- (BOOL)resignFirstResponder{
    [self setValue:[UIColor grayColor] forKeyPath:ZCPlaceholderColorKey];
    return [super resignFirstResponder];
}

#pragma mark -- 重写
//- (void)drawPlaceholderInRect:(CGRect)rect{
    //文字属性
//    NSMutableDictionary *dic = [@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:self.font}mutableCopy];
//    CGRect placeRect ;
//    placeRect.size.width = rect.size.width;
//    placeRect.size.height = self.font.lineHeight;
//    placeRect.origin.x = 0;
//    placeRect.origin.y = (rect.size.height - self.font.lineHeight) * 0.5;
//    [self.placeholder drawInRect:placeRect withAttributes:dic];
//}

@end
